package com.springboot.dubbo.base.vo;

import lombok.Getter;

import java.io.Serializable;

/**
 * 返回结果类
 * 这里采用构建者模式构建
 * 优点：
 *      1.私有化构造器访问范围小
 *      2.参数可灵活设置便于管理
 * 这部分参考部分网友代码，忘记加上地址了，抱歉，后来找不到了
 */
@Getter
public class ResultVO<T> implements Serializable {

    /**
     * 返回码
     */
    private int code = 0;
    /**
     * 返回信息
     */
    private String message = "成功";
    /**
     * 返回数据
     */
    private T data;

    /**
     * 私有化构造器
     **/
    private ResultVO() {
    }

    private ResultVO(ResultVO<T> resultVO) {
        this.code = resultVO.code;
        this.message = resultVO.message;
        this.data = resultVO.data;
    }

    /*
     * @Author 杨冬冬
     * @Description 链式调用
     * @Date 23:16 2021/7/3 0003
     * @param
     **/
    public static ResultVO.Builder builder() {
        return new ResultVO.Builder();
    }

    /**
     * Build
     */
    public static class Builder<T> {

        private ResultVO<T> resultVO;

        public Builder() {
            resultVO = new ResultVO<>();
        }

        public Builder code(int code) {
            resultVO.code = code;
            return this;
        }

        public Builder message(String message) {
            resultVO.message = message;
            return this;
        }

        public Builder data(T data) {
            resultVO.data = data;
            return this;
        }

        public ResultVO<T> build() {
            return new ResultVO<>(resultVO);
        }
    }
}
